# -*- coding:utf-8 -*-

import urllib
import urllib2
import cookielib

# request = urllib2.Request('http://www.baidu.com')
# response = urllib2.urlopen(request)
# print response.read()

"""
    zwx 2017-12-08
    基本的post、get请求方法
"""

def base_post_method():
    #定义请求参数
    values = {}
    values['userName'] = 'egova'
    values['password'] = ''
    values['ip'] = 'egova'
    values['browserVersion'] = 'chrome/61.0.3163.100'
    values['osVersion'] = 'Win10/64'
    data = urllib.urlencode(values)
    #定义请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/login/validpassword'
    #进行请求发送
    try:
        request = urllib2.Request(url,data)
        response = urllib2.urlopen(request)
        #请求结果打印
        print response.read()
    except urllib2.HTTPError,e:
        print e.code
        print e.reason
    except urllib2.URLError, e:
        print e.reason
    else:
        print "OK"

#基础的get请求
def base_get_method():
    #请求参数
    values = {}
    values['taskListID'] = '28'
    values['currentPage'] = '1'
    values['numPerPage'] = '10'
    data = urllib.urlencode(values)
    #请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/home/bizbase/tasklist/gethumantasklistdata'
    get_url = url + '?' + data
    try:
        request = urllib2.Request(get_url)
        response = urllib2.urlopen(request)
        print response.read()
    except urllib2.URLError, e:
        if hasattr(e, "code"):
            print e.code
        if hasattr(e, "reason"):
            print e.reason
    else:
        print "OK"

#header用法
def base_header_method():
    # 请求参数
    values = {}
    values['userName'] = 'egova'
    values['password'] = ''
    values['ip'] = 'egova'
    values['browserVersion'] = 'chrome/61.0.3163.100'
    values['osVersion'] = 'Win10/64'
    data = urllib.urlencode(values)
    #请求的header
    request_header = {}
    request_header['Cache-Control'] = 'no-cache, no-store, max-age=0'
    request_header['Content-Language'] = 'zh-CN'
    # request_header['Content-Type'] = 'application/json;charset=UTF-8'
    request_header['Pragma'] = 'no-cache'
    request_header['Server'] = 'Apache-Coyote/1.1'
    # request_header['Transfer-Encoding'] = 'chunked'
    # 定义请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/login/validpassword'
    # 进行请求发送
    try:
        request = urllib2.Request(url, data,request_header)
        response = urllib2.urlopen(request)
        # 请求结果打印
        print response.read()
    except urllib2.HTTPError, e:
        print e.code
        print e.reason
    except urllib2.URLError, e:
        print e.reason
    else:
        print "OK"

#cookie获取
def get_cookie_via_CookieJar():
    # 声明一个CookieJar对象实例来保存cookie
    cookie = cookielib.CookieJar()
    # 利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
    handler = urllib2.HTTPCookieProcessor(cookie)
    # 通过handler来构建opener
    opener = urllib2.build_opener(handler)
    # 此处的open方法同urllib2的urlopen方法，也可以传入request
    response = opener.open('http://www.baidu.com')
    for item in cookie:
        print 'Name = ' + item.name
        print 'Value = ' + item.value

#保存cookie到文件
def save_cookie_toFile():
    # 设置保存cookie的文件，同级目录下的cookie.txt
    filename = 'cookie.txt'
    # 声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
    cookie = cookielib.MozillaCookieJar(filename)
    # 利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
    handler = urllib2.HTTPCookieProcessor(cookie)
    # 通过handler来构建opener
    opener = urllib2.build_opener(handler)
    # 创建一个请求，原理同urllib2的urlopen
    response = opener.open("http://www.baidu.com")
    # 保存cookie到文件
    cookie.save(ignore_discard=True, ignore_expires=True)

#从文件中读取cookie
def get_cookie_fromFile():
    # 创建MozillaCookieJar实例对象
    cookie = cookielib.MozillaCookieJar()
    # 从文件中读取cookie内容到变量
    cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)
    # 创建请求的request
    req = urllib2.Request("http://www.baidu.com")
    # 利用urllib2的build_opener方法创建一个opener
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    response = opener.open(req)
    print response.read()

#把cookie保存到登录文件中，并返回相关文件
def get_login_cookie():
    filename = 'cookie.txt'
    # 声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
    cookie = cookielib.MozillaCookieJar(filename)
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    # 定义请求参数
    values = {}
    values['userName'] = 'egova'
    values['password'] = ''
    values['ip'] = 'egova'
    values['browserVersion'] = 'chrome/61.0.3163.100'
    values['osVersion'] = 'Win10/64'
    login_data = urllib.urlencode(values)
    # 定义请求接口
    login_url = 'http://192.168.32.200:8080/eUrbanMIS/login/validpassword'
    login = opener.open(login_url,login_data)
    #保存cookie
    cookie.save(ignore_discard=True, ignore_expires=True)

#读取cookie并进行案件列表获取操作
def gethumantasklistdata():
    print u'调用cookie创建文件'
    get_login_cookie()
    #进行调用
    # 创建MozillaCookieJar实例对象
    cookie = cookielib.MozillaCookieJar()
    # 从文件中读取cookie内容到变量
    cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)

    # 请求参数
    values = {}
    values['taskListID'] = '28'
    values['currentPage'] = '1'
    values['numPerPage'] = '10'
    get_data = urllib.urlencode(values)
    # 请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/home/bizbase/tasklist/gethumantasklistdata'
    get_url = url + '?' + get_data
    # 创建请求的request
    request = urllib2.Request(get_url)
    # 利用urllib2的build_opener方法创建一个opener
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    response = opener.open(request)
    print response.read()


if __name__ == '__main__':
    print "utils test"
    gethumantasklistdata()